#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

#define TOTAL 10000
#define CPU 8
#define CHILD (TOTAL/CPU)

struct param {
	int start;
	int end;
};

struct result {
	float sum;
};

void *compute(void *arg)
{
	struct param *par;
	struct result *res;
	float sum = 0;
	int i;

	par = (struct param *)arg;
	for (i = par->start; i < par->end; i++) {
		if (i % 2 == 0)
			sum += 1.0 / (2 * i + 1);
		else 
			sum -= 1.0 / (2 * i + 1);
	}
	res = malloc(sizeof(struct result));
	res->sum = sum;
	return res;
}

int main()
{
	pthread_t workers[CPU];
	struct param params[CPU];
	int i;

	for (i = 0; i < CPU; i++) {
		struct param *par;
		par = &params[i];
		par->start = i * CHILD;
		par->end = (i + 1) * CHILD;
		pthread_create(&workers[i], NULL, compute, par);
	}
        float sum = 0;
	for (i = 0; i < CPU; i++) {
		struct result *res;
		pthread_join(workers[i], (void **)&res);
		sum += res->sum;
		free(res);
	}

	printf("sum = %f\n", sum);
	return 0;
}
